package com.leyou.item.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author fenglin
 * @date 2019/12/8 17:25
 */
@Service
public class BrandService {
    @Autowired
    private BrandMapper brandMapper;

    /**
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    //   /*
    //        SELECT *
    //        FROM tb_brand
    //        WHERE `name` LIKE "%x%" OR letter = 'x'
    //        ORDER BY update_time DESC
    //        LIMIT 0, 5
    //     */
    public PageResult<BrandDTO> queryBrandByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //创建查询条件
        Example example = new Example(Brand.class);
        //关键字搜索
        // WHERE `name` LIKE "%x%"
        if (StringUtils.isNotBlank(key)){
            example.createCriteria().orLike("name", "%" + key + "%")
                    //OR letter = 'x'
                    .orEqualTo("letter", key);
        }
        //排序  注意字符串拼接一定要加空格
        String orderByClause = sortBy + (desc ? " DESC" : " ASC");
        example.setOrderByClause(orderByClause);
        //分页
        Page<Object> info = PageHelper.startPage(Math.max(1, page), rows);
        //查询结果
        List<Brand> list = brandMapper.selectByExample(example);
        //判断非空
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //转换为DTO
        List<BrandDTO> dtoList = BeanHelper.copyWithCollection(list, BrandDTO.class);
        return new PageResult<>(info.getTotal(),info.getPages(),dtoList);
    }

    /**
     * 新增品牌
     * @param brandDTO
     */
    @Transactional //事务
    public void saveBrand(BrandDTO brandDTO) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        brand.setId(null);
        brandMapper.insertSelective(brand);
        // 新增品牌和分类中间表
        brandMapper.insertCategoryBrand(brand.getId(), brandDTO.getCids());
    }

    /**
     * 根据主键查询品牌
     * @param brandId
     */
    public BrandDTO queryBrandById(Long brandId) {
        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        if (brand==null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        BrandDTO brandDTO = BeanHelper.copyProperties(brand, BrandDTO.class);
        return brandDTO;
    }

    public List<BrandDTO> queryBrandByCategory(Long id) {
       List<Brand> list=brandMapper.queryBrandByCategory(id);
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }

    public List<BrandDTO> queryByIds(List<Long> brandIdList) {
        List<Brand> list = brandMapper.selectByIdList(brandIdList);
        // 判断是否为空
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }
}
